<template>
  <el-container class="home-container">
    <!--头部-->
    <el-header>
      <div class="header-left">
        <img src="../assets/xjj.jpg" alt="这是图片">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="exit">退出</el-button>
    </el-header>
      <!--主体容器-->
    <el-container>
      <!--侧边栏-->
      <el-aside :width="isCollapse ? '64px':'200px'">
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath">
          <div class="toggle-button" @click="toggleCollapse">
            <i :class="isCollapse ?  'el-icon-s-unfold':'el-icon-s-fold'"></i>
          </div>
          <el-submenu :index="item.id +''" v-for="item in menulist" :key="item.id">
            <!--主模板-->
            <template slot="title">
              <i :class="iconListObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!--子标签-->
            <el-menu-item :index="'/'+item.path" v-for="item in item.children" :key="item.id"
                          @click="saveNavPath('/'+item.path)">
              <i class="el-icon-menu"></i>
              <span>{{item.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--主体内容-->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  created () {
    // 创建home组件的时候 拿到菜单列表
    this.getMenuList()
    // 创建home组件的时候拿到session中的activepath值 在赋值给activePath
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  data () {
    return {
      // 菜单列表
      menulist: [],
      // 一级菜单图标对象
      iconListObj: {
        125: 'iconfont  icon-users',
        103: 'iconfont  icon-tijikongjian',
        101: 'iconfont  icon-shangpin',
        102: 'iconfont  icon-danju',
        145: 'iconfont  icon-baobiao'
      },
      // 是否折叠
      isCollapse: false,
      // 默认选中菜单 从session中获取
      activePath: ''
    }
  },
  methods: {
    exit () {
      // 清空token 然后在编程式导航 跳转到login去
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        this.$message.error('接口错误')
      }
      this.menulist = res.data
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    saveNavPath (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>
<style lang="less" scoped>
  .home-container{
    height: 100%;
  }
  .el-header{
    background-color: #373D41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    .header-left{
      display: flex;
      align-items: center;
      img{
        /*width: 130px;*/
        width: 60px;
        height: 60px;
        border-radius: 50%;
        box-shadow: 0 2px 5px #eeeeee;
      }
      img:hover{
       animation: rotates 5s linear;
        animation-delay: initial;
      }
      @keyframes rotates{
        0%{transform:rotate(0deg) }
        100%{transform:rotate(360deg) }
      }
      span{
        margin-left: 15px;
        color: #eeeeee;
      }
    }
    .el-button{
      align-items: center;
    }
  }
  .el-aside{
    background-color: #333744;
    .el-menu{
      border-right: none;
    }
  }
  .el-main{
    background-color: #EAEDF1;
  }
  .iconfont{
    margin-right: 10px;
  }
  .toggle-button{
    height: 50px;
    background-color: #4A5064;
    text-align: center;
    color: #909399;
    font-size: 16px;
    /*letter-spacing: 0.2em;*/
    line-height: 50px;
    cursor: pointer;
  }
</style>
